//
//  gameover.mm
//  crazy_checkers
//
//  Created by Dmitriy Tatarinov on 11/23/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "game_const.h"
#import "gameover.h"
//#import "game.h"
//#import "game2p.h"
#import "vars.h"
#import "menu.h"

bool returnPress = false;

@implementation NameResponder
-(BOOL)textFieldShouldReturn: (UITextField *) textField
{
	if([[textField text] length] == 0) return false;
	playerName = [[NSString  alloc] initWithString:textField.text];

    [textField removeFromSuperview];
	returnPress = true;
	return true;
}
@end


void GameoverStage::Load()
{
	// load fonts
	returnPress = false;
	_mainFont             = xLoadFont("comic26");
	
	img_gameover=xLoadImage("gamover.png");
	img_name=xLoadImage("nick.png");
	img_add=xLoadImage("add.png");
	rec_on=0;
	rec_yes=0;
	
	snd_pick=xLoadSound("pick.m4a");
	
	if (soundPresent==true){
		xSoundVolume(snd_pick, 1);
	}else{
		xSoundVolume(snd_pick, 0);
	}	
	
	//грузим рекорды
	long f = xReadFile("file://score.dat");
	for(int i = 0; i < SCORE_COUNT; i++){
		strcpy(names[i], xReadLine(f));
		scores[i] = xReadInt(f);
	}
	xCloseFile(f);
	f = NULL;	
	
	// set stage as active
	MakeActive();
}

void GameoverStage::Update()
{
	if (returnPress){
		xPlaySound(snd_pick);
		Stage* st = new MenuStage();
		st->Load();
		mode=3;
		
		return;
	}
	
	int hiScore;
	
	if (numPlayers==1){
		if (total_score1>total_score2){
			hiScore=total_score1;
		}
	}else {
		if (total_score1>total_score2){
			hiScore=total_score1;
		}else{
			hiScore=total_score2;
		}
	}
	
	if (hiScore > scores[SCORE_COUNT-1]) rec_yes=1;	
	
	for(int i = 0; i < xCountTouches(); i++)
	{
		
		int x_t=xTouchX(i);
		int y_t=xTouchY(i);
		
		if(xTouchPhase(i) == 1)
		{	
			if (rec_yes==1){
			//добавить рекорд
				if (x_t>50 & x_t<270 & y_t>260 & y_t<315){	
					xPlaySound(snd_pick);
					rec_on=1;
                    Create_Textfield();
				}
			}
			//вернуться в меню
			if (x_t>50 & x_t<270 & y_t>410 & y_t<460){
				rec_on=0;
				xPlaySound(snd_pick);
				Stage* menu = new MenuStage;
				menu->Load();
			}
		}
	}
	
	//check_Touch();
	xUpdateWorld(1.0);
}

void GameoverStage::Render()
{
	
	//xRenderWorld();
	xDrawImage(img_gameover,0, 0, 0);
	if (rec_yes==0) xDrawImage(img_add, 0, 258, 0);
	if (rec_on==1)	xDrawImage(img_name, 0, 0, 0);
	// draw text information
	xSetFont(_mainFont);
	char buff[256];

	int hiScore;

	if (numPlayers==1){
		if (total_score1>total_score2){
			hiScore=total_score1;
		}
	}else {
		if (total_score1>total_score2){
			hiScore=total_score1;
		}else{
			hiScore=total_score2;
		}
	}

	sprintf(buff, "%i",hiScore);
	if (rec_on !=1) xText(115, 205, buff, false, false);
	
    xFlip();	
}

void GameoverStage::Unload()
{
	xFreeFont(_mainFont);
	xFreeImage(img_gameover);
	xFreeImage(img_name);
	xFreeImage(img_add);
	
	xFreeSound(snd_pick);
//	xFreeImage(img_dig);
	_mainFont = img_gameover = img_name = img_add = snd_pick = NULL;
	


	if (rec_on==1){
		int hiScore;
		if (numPlayers==1)
			hiScore = total_score1;
		else
			hiScore= (total_score1>total_score2?total_score1:total_score2);
		if (hiScore > scores[SCORE_COUNT-1]){
			// есть рекорд!
			int i;
			for (i = SCORE_COUNT-2; i >=0 ; i--)
				if (hiScore > scores[i]){
					scores[i+1] = scores[i];
					strcpy(names[i+1], names[i]);
				}
				else break;
			memset(names[i+1], 0, 13);
			strncpy(names[i+1], [playerName UTF8String], 12);
			scores[i+1] = hiScore;
			long f=xWriteFile("file://score.dat");
			for (i=0; i< SCORE_COUNT; i++){
				xWriteLine(f, names[i]);
				xWriteInt(f, scores[i]);
			}
			xCloseFile(f);
			f = NULL;
		}
	}
	rec_on=0;
	xFlushTouches();
}

void GameoverStage::Create_Textfield()
{
    UITextField *mytextfield=[[UITextField alloc] initWithFrame:CGRectMake(60.0, 160, 200, 32.0)];
    mytextfield.textAlignment = UITextAlignmentCenter;
  
    
    mytextfield.keyboardType = UIKeyboardTypeNamePhonePad;    
        
    mytextfield.textColor = [UIColor whiteColor];
    mytextfield.font = [UIFont systemFontOfSize:28.0f];
    mytextfield.backgroundColor = [UIColor blackColor];
     
    [Xview addSubview:mytextfield];
    [mytextfield becomeFirstResponder];
    mytextfield.delegate = [[NameResponder alloc] init];
    
    mytextfield.text = [[NSString alloc] initWithString:playerName];  

    [mytextfield release];
}
